summaryrefslogtreecommitdiff
path: root/src/pages/blog/read/[...slug].astro
blob: 71d092934c603f8c999e631469890388216f5403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
---
import { type CollectionEntry, getCollection } from "astro:content";
import { render } from "astro:content";
import Translations from "@components/Translations.astro";
import { toIso8601Full } from "@utils/datetime";
import ReadingTime from "@components/ReadingTime.astro";
import Keywords from "@components/Keywords.astro";
import Citations from "@components/Citations.astro";
import Signature from "@components/signature/Signature.astro";
import CopyrightNotice from "@components/CopyrightNotice.astro";
import { verifier as verifierPrototype } from "@lib/pgp/verify";
import { getSigners, isTranslation } from "@lib/collection/helpers";
import { get } from "@utils/anonymous";
import Authors from "@components/signature/Authors.astro";
import { getEntry } from "astro:content";
import Base from "@layouts/Base.astro";
import readingTime from "reading-time";
import type {
  Entry,
  MicroEntry,
  OriginalEntry,
} from "@lib/collection/schemas";

export async function getStaticPaths() {
  const posts = await getCollection("blog");
  return posts.map((post) => ({
    params: { slug: post.id },
    props: post,
  }));
}

type Props = CollectionEntry<"blog">;

const post = Astro.props;

let original: OriginalEntry | MicroEntry;
if (isTranslation(post)) {
  original = await getEntry(post.data.translationOf) as
    | OriginalEntry
    | MicroEntry;

  if (!original) {
    throw new Error(`Original post not found for ${post.id}`);
  }

  const originalAuthor = (original.data.signers ?? []).filter(
    (s) => s.role === "author",
  ).map((s) => s.entity.id)?.[0];
  const originalCoAuthors = new Set(
    (original.data.signers ?? []).filter(
      (s) => s.role === "co-author",
    ).map((s) => s.entity.id),
  );
  const translationAuthor = (post.data.signers ?? []).filter(
    (s) => s.role === "author",
  ).map((s) => s.entity.id)?.[0];
  const translationCoAuthors = new Set(
    (post.data.signers ?? []).filter(
      (s) => s.role === "co-author",
    ).map((s) => s.entity.id),
  );

  if (
    (translationAuthor !== undefined &&
      translationAuthor !== originalAuthor) ||
    !translationCoAuthors.isSubsetOf(originalCoAuthors)
  ) {
    throw new Error(
      `Post ${post.id} has mismatched (co-)authors from original post ${original.id}`,
    );
  }

  const translators = (post.data.signers ?? []).filter(
    (s) => s.role === "translator",
  ).map((s) => s.entity.id);

  for (const translator of translators) {
    if (
      originalAuthor === translator || originalCoAuthors.has(translator)
    ) {
      throw new Error(
        `Translator ${translator} in ${post.id} is already a (co-)author in original post`,
      );
    }
  }
} else {
  original = post;
  if (post.data.signers?.some((x) => x.role === "translator")) {
    throw new Error(
      `Post ${post.id} is not a translation but has translators defined`,
    );
  }
}

// Add own post as a translation
const translationsSet = new Set(
  (await getCollection(
    "blog",
    (x) =>
      (x.data.kind === "translation") && x.data.translationOf.id ===
        (post.data.kind === "translation"
          ? post.data.translationOf.id
          : post.id),
  ) ?? []).map(({ id }) => id),
);

translationsSet.add(post.id);
const translations = [...translationsSet.values()].map((id) => ({
  collection: post.collection,
  id,
}));

const signers = await getSigners(post);

const verifier = await verifierPrototype.then((x) => x.clone());

// Add signers public keys to keyring
for (const { data } of signers.map(get("entity"))) {
  if (data.publickey.armor !== undefined) {
    verifier.addKeyFromArmor(data.publickey.armor);
  }
}

const verification = post.filePath !== undefined
  ? await verifier.verify([
    new URL(`file://${Deno.cwd()}/${post.filePath}`),
  ])
  : undefined;

const { Content } = await render(post);

const { lang } = post.data;

const commit = await verification?.commit;

const reading = post.body ? readingTime(post.body, {}) : undefined;
const minutes = reading === undefined
  ? undefined
  : Math.ceil(reading.minutes);
const estimative = reading === undefined
  ? undefined
  : new Intl.DurationFormat(lang, {
    style: "long",
  }).format({ minutes });
const duration = minutes === undefined
  ? undefined
  : `PT${Math.floor(minutes / 60) > 0 ? Math.floor(minutes / 60) + "H" : ""}${
    minutes % 60 > 0 ? minutes % 60 + "M" : ""
  }`;

const getOrUndefined = (k: string) =>
  k in post.data ? post.data[k as keyof typeof post.data] : undefined;
const author = {
  "@type": "Person",
} as const;
const contributor = post.data.signers.filter(({ role }) =>
  role === "co-author"
).map(() => {
  return {
    "@type": "Person",
  } as const;
});
const translator = post.data.signers.filter(({ role }) =>
  role === "translator"
).map(() => {
  return {
    "@type": "Person",
  } as const;
});
const JSONLD = {
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "@id": Astro.url.href,
  articleBody: post.rendered?.html ?? post.body,
  abstract: getOrUndefined("description"),
  alternativeHeadline: getOrUndefined("subtitle"),
  author,
  citation: [].map(() => {
    return {
      "@type": "CreativeWork",
    };
  }),
  contributor,
  copyrightHolder: [author, ...contributor, ...translator],
  // copyrightNotice: post.data.license, // WORKAROUND
  copyrightYear: post.data.dateCreated.getFullYear(),
  creativeWorkStatus: "Published",
  dateCreated: post.data.dateCreated.toISOString(),
  dateModified: "dateUpdated" in post.data
    ? post.data.dateUpdated?.toISOString()
    : undefined,
  // datePublished: undefined, // from git commit commit date
  encodingFormat: "text/html",
  headline: post.data.title,
  inLanguage: post.data.lang,
  isAccessibleForFree: true,
  isBasedOn: isTranslation(post)
    ? {
      "@type": "BlogPosting",
      "@id": new URL(`blog/read/${post.data.translationOf}`, Astro.site).href,
    }
    : undefined,
  keywords: original.data.keywords,
  license: post.data.license, // WORKAROUND
  locationCreated: {
    "@type": "Place",
    // XXX: getOrUndefined("locationCreated"),
  },
  mentions: [].map(() => {
    return {
      "@type": "Thing",
    };
  }),
  // publication: {
  //   "@type": "PublicationEvent",
  // }, // from git commit
  // publisher: {
  //   "@type": "Person",
  // }, // from git commit
  text: post.rendered?.html ?? post.body,
  timeRequired: post.body !== undefined ? duration : undefined,
  translationOf: isTranslation(post)
    ? {
      "@type": "BlogPosting",
      "@id": new URL(`blog/read/${post.data.translationOf}`, Astro.site).href,
    }
    : undefined,
  translator,
  // version: undefined // TODO
  wordCount: reading?.words,
  workTranslations: translations.filter(({ id }) => id !== post.id).map((
    { id },
  ) => ({
    "@type": "BlogPosting",
    "@id": new URL(`blog/read/${id}`, Astro.site).href,
  })),
  description: getOrUndefined("description"),
  name: post.data.title,
  url: Astro.url.href,
} as const;
---

<Base
  title={post.data.title}
  description={"description" in post.data ? post.data.description : post.data.title}
>
  <main>
    <article
      itemscope
      itemtype="http://schema.org/BlogPosting"
      itemid={Astro.url.href}
    >
      <Translations {translations} {lang} />
      <hgroup>
        <h1 itemprop="headline">{post.data.title}</h1>
        {
          "subtitle" in post.data && (
            <p itemprop="alternativeHeadline" class="subtitle">
              {post.data.subtitle}
            </p>
          )
        }
      </hgroup>
      {
        "description" in post.data && post.data.description &&
  (
          <section itemprop="abstract">
            <h2>Resumo</h2>
            {
              post.data.description.split(new RegExp("\\s{2,}"))
                .map((
                  x,
                ) => <p>{x}</p>)
            }
          </section>
        )
      }
      {verification && <Signature {lang} {verification} />}
      <footer>
        {
          verification?.verifications &&
  (
            <Authors
              verifications={verification.verifications}
              expectedSigners={signers}
              commitSignerKey={commit?.signature?.signer}
            />
          )
        }
        <dl>
          <dt>Data de criação</dt>
          <dd>
            <time
              itemprop="dateCreated"
              datetime={toIso8601Full(post.data.dateCreated)}
            >{
              new Intl.DateTimeFormat([lang], {}).format(
                post.data.dateCreated,
              )
            }</time>
          </dd>
          {
            post.data.dateUpdated && (
              <dt>Última atualização</dt><dd>
                <time
                  itemprop="dateModified"
                  datetime={toIso8601Full(post.data.dateUpdated)}
                >{
                  new Intl.DateTimeFormat([lang], {}).format(
                    post.data.dateUpdated,
                  )
                }</time>
              </dd>
            )
          }
          {
            "locationCreated" in post.data &&
              post.data.locationCreated && (
              <dt
                itemprop="locationCreated"
                itemscope
                itemtype="https://schema.org/Place"
              >
                Local de criação
              </dt><dd>
                <span itemprop="name">{post.data.locationCreated}</span>
              </dd>
            )
          }
        </dl>
        <ReadingTime body={post.body} {lang} />
      </footer>
      <hr />
      <div itemprop="articleBody text"><Content /></div>
      <hr />
      {
        "keywords" in original.data && (
          <Keywords keywords={original.data.keywords} />
        )
      }
      {
        "relatedPosts" in original.data && (
          <Citations citations={original.data.relatedPosts} />
        )
      }
      <CopyrightNotice
        author={signers[0]?.entity.data.websites?.[0] ?? "Anonymous"}
        website={signers[0]?.entity.data.websites?.[0]}
        email={signers[0]?.entity.data.websites?.[0]}
        title={post.data.title}
        dateCreated={post.data.dateCreated}
        license={post.data.license}
      />
    </article>
  </main>
</Base>

<script
  type="application/ld+json"
  is:inline
  set:html={JSON.stringify(JSONLD)}
/>

<script type="module" is:inline>
  hashchange();

  window.addEventListener("hashchange", hashchange);

  document.addEventListener(
    "click",
    function (event) {
      if (
        event.target &&
        event.target instanceof HTMLAnchorElement &&
        event.target.href === location.href &&
        location.hash.length > 1
      ) {
        requestIdleCallback(function () {
          if (!event.defaultPrevented) {
            hashchange();
          }
        });
      }
    },
    false,
  );

  function hashchange() {
    let hash;

    try {
      hash = decodeURIComponent(location.hash.slice(1)).toLowerCase();
    } catch (e) {
      return;
    }

    const name = "user-content-" + hash;
    const target = document.getElementById(name) ||
      document.getElementsByName(name)[0];

    if (target) {
      requestIdleCallback(function () {
        target.scrollIntoView();
      });
    }
  }
</script>

<style is:inline>
  section[data-footnotes].footnotes {
    word-wrap: break-word;
  }
</style>

<style>
  hgroup {
    text-align: center;
  }

  .subtitle {
    font-weight: lighter;
  }

  [itemprop~="articleBody"] {
    line-height: 1.4;
    font-size: 1.2em;
    text-align: justify;

    & h1,
    & h2,
    & h3 {
      line-height: 1.2;
    }
  }

  [itemprop="abstract"] {
    margin-inline: 1em;
    padding-block: 1em;
    font-style: italic;
  }

  @media print {
    body {
      font-size: 1rem;
      font-family: var(--ff-serif);
      line-height: 1.62;
    }
  }
</style>